package com.lun.blog.service.impl;

import com.lun.blog.repostory.FriendLinksRepository;
import com.lun.blog.pojo.FriendLinks;
import com.lun.blog.service.FriendLinksService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class FriendLinksServiceImpl implements FriendLinksService {
    @Autowired
    private FriendLinksRepository friendLinksRepository;
    @Override
    public FriendLinks getFriendLinks(Long id) {
        return friendLinksRepository.getOne(id);
    }

    @Override
    public Page<FriendLinks> listPage(Pageable pageable) {
        return friendLinksRepository.findAll(pageable);
    }

    @Override
    @Transactional
    public FriendLinks saveFriendLinks(FriendLinks friendLinks) {
        if(friendLinks.getId()!=null){
            friendLinks.setUpdateTime(new Date());
        }else {
            friendLinks.setCreateTime(new Date());
            friendLinks.setUpdateTime(new Date());
        }
        return friendLinksRepository.save(friendLinks);
    }

    @Override
    @Transactional
    public boolean deleteFriendLinks(Long id) {
        FriendLinks one = friendLinksRepository.getOne(id);
        if(one==null){
            return false;
        }
        friendLinksRepository.delete(one);
        return true;
    }

    @Override
    public FriendLinks findByBlogName(String blogName) {
        return friendLinksRepository.findByBlogName(blogName);
    }

    @Override
    public List<FriendLinks> findAllFriendLinks() {
        return friendLinksRepository.findAll();
    }
}
